$(function() {
	savehandle();
	changeHeaderMenu(0);
	changeMenu(0, 1);
	getCity();
});
function getCity(){
	$.getJSON('/fw/pages/agent/js/citys.json').done(function(data){
		var data = data.result.rows;
		var elementById = document.getElementById("province");
		for ( var int = 0; int < data.length; int++) {
			var d = data[int];
			var province = d.mainCity;
			var op=document.createElement("option");      
			op.setAttribute("class","province_o");        
			op.setAttribute("value",province);         
			op.appendChild(document.createTextNode(province));
			elementById.appendChild(op);         
		}
	});
}
function provinceChange(){
	var province = $('.province').val();
	$.getJSON('/fw/pages/agent/js/citys.json').done(function(data){
		var data = data.result.rows;
		var elementById = document.getElementById("city");
		elementById.options.length=0;
		for ( var int = 0; int < data.length; int++) {
			var d = data[int];
			var provinces = d.mainCity;
			if(province == provinces){
				var city = d.citys;
				for ( var i = 0; i < city.length; i++) {
					var op=document.createElement("option");      
					op.setAttribute("class","city_o");        
					op.setAttribute("value",city[i].name);         
					op.appendChild(document.createTextNode(city[i].name));
					elementById.appendChild(op);         
				}
				break;
			}
		}
	});
}
function successCallBack() {
	window.location.href = 'inCafeList.jsp';
}

/**
 * 提示信息处理
 */
function savehandle() {
	$('#alertdiv').hide();
	$('#alertdiv').savehandle( {
		fieldMap : {},
		successMsg : '添加成功！',
		failMsg : '添加失败！错误：',
		success : successCallBack
	});
}
function retInCofe(){
	//$('#agentName').val("");
	$('#name').val("");
	$('#sn').val("");
	$('#phone1').val("");
	$('#phone2').val("");
	$('#addr').val("");
	$('#user').val("");
	$('#email').val("");
}

function addInCofe() {
//	var agentName = $('#agentName').val();
//	if (!agentName) {
//		$('#alertdiv').savehandle('setmsg', {msg : '请输入代理商名称!',timer : 2000});
//		return;
//	}
	var name = $('#name').val();
	if (!name) {
		$('#alertdiv').savehandle('setmsg', {msg : '请输入网吧/企业名称!',timer : 2000});
		return;
	}
	var sn = $('#sn').val();
	if (!sn) {
		$('#alertdiv').savehandle('setmsg', {msg : '请输入设备编号!',timer : 2000});
		return;
	}
	/*var map = {"fwsn":sn}
		$.getJSON('/fw/agent/agentSro_findbyFwSN.action', map, function(data) {
				if (!sn) {
					$('#alertdiv').savehandle('setmsg', {msg : '请输入设备编号!',timer : 2000});
					return;
				}
				if(data != "0"){
					if(data.sn != null){
						sn = data.sn;
					}
				}*/
				/*if(data == "0"){
					$('#alertdiv1').savehandle('setmsg', {msg : '当前设备编号可用!',timer : 2000});
				}else {
					var sn = $('#sn').val(data.sn);
					$('#alertdiv1').savehandle('setmsg', {msg : '以为您匹配当前所对应设备编号!',timer : 2000});
				}
				*/
//	$.getJSON('/fw/agent/agentDev_findbySN.action', {"sn":sn}, function(data) {
//		var r = data.result;
//		if(r.total != 0){
//			var name = data.result.rows[0].name;
//			alert("该设备已关联到“" + name + "”网吧/企业");
//			return;
//		}
//	});
	var phone1 = $('#phone1').val();
	if(!phone1){
		phone1 = "";
	}
	var phone2 = $('#phone2').val();
	if(!phone2){
		phone2 = "";
	}
	var addr = $('#addr').val();
	var province = $('#province option:selected').html();
	var city = $('#city option:selected').html();
	if(city.indexOf('请选择') == -1)
		addr = city +"-"+ addr;
	if(province.indexOf('请选择') == -1)
		addr = province + "-" + addr;
	if(!addr){
		addr = "";
	}
	var user = $('#user').val();
	if(!user){
		user = "";
	}
	var email = $('#email').val();
	if(!email){
		email = "";
	}
console.log(sn);
	var data = {"name":name,"sn":sn,
			    "phone1":phone1,"phone2":phone2,"addr":addr,
			    "user":user,"email":email};
	$.getJSON('/fw/agent/agentDev_addInCafe.action', data, function(data) {
		if (data.success) {
			$('#alertdiv').savehandle('setmsg', {
				msg : '成功!',
				timer : 2000
			});
			window.location.href = 'inCafeList.jsp';
		} else {
			var msg = data.result;
			$('#alertdiv').savehandle('setmsg', {
				msg : msg.errors,
				timer : 200000
			});
		}
	});
}
